iOS如何避免App Crash

获取崩溃信息

1
2
3
4
5
6
7
8
9
10
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSSetUncaughtExceptionHandler(&uncaughtExceptionHandler);
}

void uncaughtExceptionHandler(NSException *exception) {

NSLog(@"CRASH: %@", exception);
NSLog(@"Stack Trace: %@", [exception callStackSymbols]);
}
1
2
3
4
5
6
-(BOOL) isValueOfKeyIsNull(id response){
if([response==[NSNull null]]){
return true;
}
return false;
}

使用@try...@catch避免崩溃

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
-(void)functionInsideWhichAppIsCrashing
{
NSArray* arraytest = @[@"1",@"2"];
@try
{
//Your crashing code goes here
NSLog(@"Object: %@",arraytest[3]);
}
@catch (NSException *exception)
{
// Print exception information
NSLog( @"NSException caught" );
NSLog( @"Name: %@", exception.name);
NSLog( @"Reason: %@", exception.reason );
}
@finally
{
// // Do whatever you want to do in crash situation
NSLog( @"In finally block");
}
}
1
2
3
+ (BOOL)nullValue:(id)value {
return ((NSNull *)value == [NSNull null] || [@"<null>" isEqualToString:(NSString *)value] || [@"(null)" isEqualToString:(NSString *)value] || value == nil);
}

https://github.com/MarkQSchultz/QZObserver

https://github.com/PonyCui/KVSafe

https://github.com/wuwen1030/XTSafeCollection

https://github.com/jasenhuang/NSObjectSafe

https://github.com/deput/RWJSONAid

https://github.com/xuvw/HKSafePush

https://github.com/bigParis/SwizzlingMethod

https://github.com/LQQZYY/safetyDictionaryDemo/tree/master/NSDictionaryDemo

https://github.com/oenius/YGSafeKVO/tree/master/YGKVO

https://github.com/allenhsu/NSDictionary-Accessors

https://github.com/shaojiankui/NSDictionary-SafeAccess

https://github.com/gdier/SafeKVObject

https://github.com/fengchuanxiang/SafeCollections

https://github.com/LuKing4DB/SafeEX

https://github.com/MarkQSchultz/QZObserver

https://github.com/crazyhf/SafeKVOHandle

文章作者: kyren
文章链接: http://huluo666.github.io/2016/05/03/iOS如何避免App Crash/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Kyren's Blog